
import turtle
from datetime import time
from time import sleep

from cv2.cv2 import waitKey


def drawBPH(mybyte,BPHwidth,left,top,LF):


    turtle.setup(800, 600, 0, 0)
    turtle.pensize(3)  # 画笔尺寸
    turtle.pencolor("green")
    turtle.seth(0)  # 前进的方向
    turtle.delay(0)


    # RECT
    turtle.penup();
    turtle.goto(left , top )
    turtle.pendown();
    turtle.forward(BPHwidth)
    turtle.right(90)
    turtle.forward(BPHwidth)
    turtle.right(90)
    turtle.forward(BPHwidth)
    turtle.right(90)
    turtle.forward(BPHwidth)

    #mybyte = 0xA5
    byte=mybyte
    bit = [[False] for i in range(8)]

    for i in range(8):
        if int(byte / (2 ** (7 - i))) > 0:
            bit[7 - i] = True
            #print("ok", byte, 2 ** (7 - i))
            byte = byte % (2 ** (7 - i))
        #print(byte, i, bit[7 - i])

    for i in range(8):
        turtle.penup()
        if bit[i] == True:
            turtle.goto(left+BPHwidth / 10 * (1 + 7 - i), top-BPHwidth / 10 * 3)
            turtle.seth(-90)  # 前进的方向
            turtle.pendown()
            turtle.forward(BPHwidth / 10 * 4)
        else:
            turtle.goto(left+BPHwidth / 10 * (1 + 7 - i), top-BPHwidth / 10 * 4)
            turtle.seth(-90)  # 前进的方向
            turtle.pendown()
            turtle.forward(BPHwidth / 10 * 2)

    # L
    turtle.penup()
    turtle.goto(left+BPHwidth / 10 * 1, top-BPHwidth / 10 * 8)
    turtle.pendown()
    turtle.seth(-90)  # 前进的方向
    turtle.forward(BPHwidth / 10 * 1)
    turtle.left(90)
    turtle.forward(BPHwidth / 10 * 6.5)
    # F
    if LF=="F":
        turtle.penup()
        turtle.goto(left+BPHwidth / 10 * 2, top-BPHwidth / 10 * 8)
        turtle.pendown()
        turtle.seth(-90)  # 前进的方向
        turtle.forward(BPHwidth / 10 * 1)
    #
    turtle.hideturtle()
    #text
    if LF == "L":
        turtle.penup();
        turtle.goto(left + 10, top + 10);
        turtle.pendown();
        turtle.write((hex(mybyte)), False, "center");
    #
    if LF == "F":
        turtle.penup();
        turtle.goto(left + 10, top - 20 - BPHwidth);
        turtle.pendown();
        turtle.write(str(mybyte), False, "center");



if __name__ == "__main__":



       #s = input("input:")
       s="hello world"
       encoded = s.encode('utf-8')
       array = bytearray(encoded)
       BPHw=60
       for i in range(len(array)):
           drawBPH(array[i], BPHw, -400+i * (1.1*BPHw) + 10, +260-10, "L")
           # drawBPH(107,100,10+100+10,-10,"F")
       turtle.penup();
       turtle.goto(-400+ 10, 260+20);
       turtle.pendown();
       turtle.write(s, False, "left")#"center");
       turtle.exitonclick()